<template>
  <div>
    <h1>演示数据共享 {{ sum }}</h1>
    <input type="text" v-model="name" placeholder="请输入姓名">
    <button @click="addPerson">点击给vuex中添加一个学生信息</button>
    
    <ul>
        <li v-for="person in persons" :key="person.id">{{ person.name }}</li>
    </ul>
  </div>
</template>

<script>
import { nanoid } from 'nanoid'

    export default {
        name: "Person",
        data: function(){
            return {
                name: '',
                // persons:[
                //     {id:1,name:'张三'},
                //     {id:2,name:'李四'},
                //     {id:3,name:'王五'}
                // ]
            }
        },
        computed:{
            sum:{
                get(){
                    return this.$store.state.sum
                }
            },
            persons:{
                get(){
                    return this.$store.state.persons
                }
            }
        },
        methods:{
            addPerson(){
                //创建一个person对象
                const person= {
                    id: nanoid(),
                    name: this.name
                }
                this.$store.commit('ADD_PERSON',person)
            }
        }
    }
</script>

<style>

</style>